package com.bnyy.common.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.common.R;


public class FormInfoItem extends FrameLayout {

    public static final int READ_ONLY = 0, PRESS = 1, INPUT = 2, PHONE = 3, LOCATION = 4, SWITCH = 5;
    Context context;
    TextView tvTitle;
    EditText etContent;
    boolean contentBold;

    LayoutInflater inflater;
    String title, hint, content;
    int type = 2, contentColor, titleColor, hintColor;
    boolean hideDivider, mustInput;
    int grayText;
    int itemTitleDrawableStart;
    int itemTitleDrawableEnd;

    public FormInfoItem(@NonNull Context context) {
        super(context);
        init(context);
    }

    public FormInfoItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FormInfoItem);
        title = a.getString(R.styleable.FormInfoItem_itemTitle);
        hint = a.getString(R.styleable.FormInfoItem_itemHint);
        content = a.getString(R.styleable.FormInfoItem_itemText);
        contentColor = a.getColor(R.styleable.FormInfoItem_itemContentColor, getResources().getColor(R.color.gray_text));
        titleColor = a.getColor(R.styleable.FormInfoItem_itemTitleColor, getResources().getColor(R.color.gray_text));
        hintColor = a.getColor(R.styleable.FormInfoItem_itemHintColor, -1);
        type = a.getInt(R.styleable.FormInfoItem_type, INPUT);
        hideDivider = a.getBoolean(R.styleable.FormInfoItem_hideDivider, false);
        mustInput = a.getBoolean(R.styleable.FormInfoItem_itemMustInput, false);
        itemTitleDrawableStart = a.getResourceId(R.styleable.FormInfoItem_itemTitleDrawable, -1);
        itemTitleDrawableEnd = a.getResourceId(R.styleable.FormInfoItem_itemTitleDrawableEnd, -1);
        contentBold = a.getBoolean(R.styleable.FormInfoItem_itemContentBold, false);
        a.recycle();

        init(context);
    }

    private void init(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.layout_form_info_item, this, true);
        grayText = getResources().getColor(R.color.gray_text);

        etContent = findViewById(R.id.et_content);
        etContent.setTextColor(contentColor);
        if (hintColor != -1) {
            etContent.setHintTextColor(hintColor);
        }

        if (contentBold) {
            etContent.getPaint().setFakeBoldText(true);
        }

        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText(title);
        tvTitle.setTextColor(titleColor);

        if (hideDivider) {
            findViewById(R.id.divider).setVisibility(GONE);
        }
        setType(type);

        if (!TextUtils.isEmpty(content)) {
            etContent.setText(content);
        }

        Drawable drawableStart = null, drawableEnd = null;
        if (itemTitleDrawableEnd != -1) {
            drawableEnd = context.getResources().getDrawable(itemTitleDrawableEnd);
            drawableEnd.setBounds(0, 0, drawableEnd.getMinimumWidth(), drawableEnd.getMinimumHeight());
        }
        if (mustInput) {
            drawableStart = getResources().getDrawable(R.mipmap.icon_red_star);
            drawableStart.setBounds(0, 0, drawableStart.getMinimumWidth(), drawableStart.getMinimumHeight());

        } else {
            if (itemTitleDrawableStart != -1) {
                drawableStart = context.getResources().getDrawable(itemTitleDrawableStart);
                drawableStart.setBounds(0, 0, drawableStart.getMinimumWidth(), drawableStart.getMinimumHeight());
            }
        }
        if (drawableStart != null || drawableEnd != null) {
            tvTitle.setCompoundDrawablePadding(10);
            tvTitle.setCompoundDrawables(drawableStart, null, drawableEnd, null);
        }
    }

    public EditText getEtContent() {
        return etContent;
    }

    public void setOnPressListener(OnClickListener l) {
        if (l != null) {
            etContent.setOnClickListener(l);
            setOnClickListener(l);
        }
    }

    public void setOnPressListener(final OnPressListener l) {
        if (l != null) {
            etContent.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    l.onPress(FormInfoItem.this, tvTitle, etContent);
                }
            });
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    l.onPress(FormInfoItem.this, tvTitle, etContent);
                }
            });
        }
    }

    public void setContent(String content) {
        etContent.setText(TextUtils.isEmpty(content) ? "" : content);
    }

    public void setType(int type) {
        this.type = type;
        switch (type) {
            case READ_ONLY:
                etContent.setHint("");
                etContent.setEnabled(false);
                etContent.setFocusableInTouchMode(false);//不可编辑
                etContent.setCompoundDrawables(null, null, null, null);
                break;
            case PRESS:
                Drawable arrow = context.getResources().getDrawable(R.mipmap.icon_arrow_right_black);
                arrow.setBounds(0, 0, arrow.getMinimumWidth(), arrow.getMinimumHeight());
                etContent.setCompoundDrawables(null, null, arrow, null);
                etContent.setCompoundDrawablePadding(SizeUtils.dp2px(5));
                etContent.setEnabled(true);
                etContent.setFocusable(false);
                etContent.setFocusableInTouchMode(false);//不可编辑
                etContent.setHint(TextUtils.isEmpty(hint) ? "请选择" : hint);
                break;
            case INPUT:
                etContent.setEnabled(true);
                etContent.setFocusableInTouchMode(true);
                etContent.setHint(TextUtils.isEmpty(hint) ? "请输入" : hint);
                break;
            case PHONE:
//                etContent.setTextColor(getResources().getColor(R.color.blue));
//                etContent.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (context instanceof Activity) {
//                            String number = etContent.getText().toString();
//                            PopupWindowHelper.getInstance((AppCompatActivity) context).showCallPopupWindow(number);
//                        }
//                    }
//                });
                break;
            case LOCATION:
//                Drawable goTo = context.getResources().getDrawable(R.mipmap.icon_go_to);
//                goTo.setBounds(0, 0, goTo.getMinimumWidth(), goTo.getMinimumHeight());
//                etContent.setCompoundDrawables(goTo, null, null, null);
//                etContent.setKeyListener(null);
//                etContent.setFocusable(false);
//                etContent.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//
//                    }
//                });
                break;
        }
    }

    public int getType() {
        return type;
    }

    public void setTitle(String title) {
        this.title = title;
        tvTitle.setText(title);
    }

    public String getTitle() {
        return title;
    }

    public void setHint(String hint) {
        this.hint = hint;
        etContent.setHint(hint);
    }

    public void setEtContentMaxLength(int maxLength) {
        etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
    }

    public String getHint() {
        return etContent.getHint().toString();
    }

    public void setInputType(int inputType) {
        etContent.setInputType(inputType);
    }

    public String getContent() {
        //        if (TextUtils.isEmpty(content)) {
//            Toast.makeText(context, etContent.getHint().toString() + tvTitle.getHint().toString(), Toast.LENGTH_SHORT).show();
//        }
        Editable etContentText = etContent.getText();
        return TextUtils.isEmpty(etContentText) ? "" : etContentText.toString();
    }

    public int getContentColor() {
        return contentColor;
    }

    public void setContentColor(int contentColor) {
        this.contentColor = contentColor;
        etContent.setTextColor(contentColor);
    }

    public interface OnPressListener {
        void onPress(FormInfoItem formInfoItem, TextView tvTtile, EditText tvContent);
    }

    public static boolean checkFormInfoItem(Context context, FormInfoItem... formInfoItems) {
        for (FormInfoItem formInfoItem : formInfoItems) {
            if (TextUtils.isEmpty(formInfoItem.getContent())) {
                Toast.makeText(context, formInfoItem.getHint() + formInfoItem.getTitle(), Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }
}






























